import {
  createRouter,
  createWebHashHistory,
  createWebHistory
} from 'vue-router'

import {
  http
} from '../utils/http/index'

const routes = [{
    path: '/',
    name: "Web",
    component: () => import('../views/Web/index.vue')
  },
  {
    path: '/detail',
    name: "Detail",
    component: () => import('../views/Web/detail.vue')
  },
  {
    path: '/admin',
    name: "Admin",
    component: () => import('../views/Home/index.vue'),
    children: [{
        path: '/admin/users/personal',
        name: "UserPersonal",
        component: () => import('../views/User/personal.vue'),
        meta: {
          tile: '个人资料'
        }
      },
      {
        path: '/admin/users/password',
        name: "UserPassword",
        component: () => import('../views/User/password.vue'),
        meta: {
          tile: '修改密码'
        }
      },
      {
        path: '/admin/article',
        name: "AdminArticle",
        component: () => import('../views/Article/index.vue'),
        meta: {
          tile: '文章列表'
        }
      },
      {
        path: '/admin/article/add',
        name: "ArticleAdd",
        component: () => import('../views/Article/add.vue'),
        meta: {
          tile: '发布文章'
        }
      },
      {
        path: '/admin/article/update',
        name: "ArticleUpdate",
        component: () => import('../views/Article/update.vue'),
        meta: {
          tile: '更新文章'
        }
      },
      {
        path: '/admin/comment',
        name: "Comment",
        component: () => import('../views/Comment/index.vue'),
        meta: {
          tile: '文章评论'
        }
      },
      {
        path: '/admin/fans',
        name: "Fans",
        component: () => import('../views/Fans/index.vue'),
        meta: {
          tile: '粉丝管理'
        }
      },
      {
        path: '/admin/star',
        name: "Star",
        component: () => import('../views/Star/index.vue'),
        meta: {
          tile: '点赞管理'
        }
      }
    ]
  },
  {
    path: '/login',
    name: "Login",
    component: () => import('../views/Login/index.vue')
  },
]

const router = createRouter({
  // history: createWebHashHistory(),
  history: createWebHistory(),
  routes
})


router.beforeEach(async (to, from) => {
  if (to.path === '/login') {
    return true
  }
  try {
    let res = await http.post('/user/verify')
    if (res.code === 200) {
      return true
    } else {
      return false
    }
  } catch (err) {
    router.push({
      name: "Login"
    })
  }

})




export default router